<script type="text/html" data-template-name="timer">
    <div class="form-row">
        <label for="node-input-name"><i class="fa fa-tag"></i> <span data-i18n="common.label.name"></span></label>
        <input type="text" id="node-input-name" data-i18n="[placeholder]common.label.name">
    </div>

    <div class="form-row">
        <label for="node-input-cron"><i class="fa fa-calendar-times-o"></i> <span>cron表达式</span></label>
        <input type="text" id="node-input-cron" placeholder="e.g. 0/10 * * * * ?">
    </div>

    <div class="form-row">
        <label for="node-input-scheduleRule"><i class="fa fa-server"></i> <span>调度规则</span></label>
        <input type="text" id="node-input-scheduleRule">
    </div>

    <div class="form-row">
        <i class="fa fa-calculator"></i> <span>未来20次运行时间</span>
        <br><br>
        <code id="las-cron-execute-time">

        </code>
    </div>

</script>

<script type="text/javascript">
    (function () {
        function validateCron(cron) {
            var arr = cron.split(" ");

            return arr.length === 6 && arr[arr.length - 1] !== '';
        }

        RED.nodes.registerType('timer', {
            category: 'common',
            name: "定时任务",
            color: "#E6E0F8",
            defaults: {
                name: {value: ""},
                scheduleRule: {type: "schedule-rule", value: "",required: false},
                cron: {
                    value: "", required: true, validate: validateCron
                }
            },
            inputs: 0,
            outputs: 1,
            icon: "timer.svg",
            label: function () {
                return this.name || "定时";
            },
            labelStyle: function () {
                return this.name ? "node_label_italic" : "";
            },
            oneditprepare: function () {
                function loadLastExecuteTime(val) {
                    $.ajax({
                        "url": RED.settings.apiBaseUrl + "/rule-engine/utils/cron/times?cron=" + val + "&times=20"
                    }).then(function (res) {
                        $("#las-cron-execute-time")
                            .html(
                                res.result.join("<br/>")
                            )
                    })
                }

                $("#node-input-cron").bind("input propertychange", function () {
                    var val = $(this).val();
                    $("#las-cron-execute-time").html("请输入正确的cron表达式");
                    if (validateCron(val)) {
                        loadLastExecuteTime(val);
                    }
                });
                if (this.cron) {
                    loadLastExecuteTime(this.cron);
                }
            },
            oneditsave: function () {

            }
        });
    })()
</script>
